package com.gxuwz.attend.dao.instructor;

import com.gxuwz.attend.entity.Course; // 引入 Course 实体类
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class CourseDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<Course> getAllCourses(int size, int offset) {
        String sql = "SELECT * FROM sys_course LIMIT ? OFFSET ?";
        return jdbcTemplate.query(sql, new Object[]{size, offset}, (rs, rowNum) -> {
            Course course = new Course();
            course.setCourseID(rs.getString("courseID"));
            course.setClassID(rs.getString("classID"));
            course.setCourseName(rs.getString("courseName"));
            course.setYear(rs.getString("year"));
            course.setTerm(rs.getString("term"));
            course.setHour(rs.getInt("hour"));
            return course;
        });
    }

    public void addCourse(Course course) {
        String sql = "INSERT INTO sys_course(courseID, classID, courseName, year, term, hour) VALUES(?, ?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql, course.getCourseID(), course.getClassID(), course.getCourseName(), course.getYear(), course.getTerm(), course.getHour());
    }

    public boolean deleteCourse(String courseID) {
        String sql = "DELETE FROM sys_course WHERE courseID=?";
        int rs = jdbcTemplate.update(sql, courseID);
        return rs > 0; // 返回是否删除成功
    }

    public boolean updateCourse(Course course) {
        String sql = "UPDATE sys_course SET classID=?, courseName=?, year=?, term=?, hour=? WHERE courseID=?";
        int rs = jdbcTemplate.update(sql, course.getClassID(), course.getCourseName(), course.getYear(), course.getTerm(), course.getHour(), course.getCourseID());
        return rs > 0; // 返回是否更新成功
    }

    public List<Course> findCourses(String courseID, String courseName, int size, int offset) {
        // 构建基本的 SQL 查询
        StringBuilder sql = new StringBuilder("SELECT * FROM sys_course WHERE 1=1");
        List<Object> params = new ArrayList<>();
        // 根据课程ID模糊查询
        if (courseID != null && !courseID.isEmpty()) {
            sql.append(" AND courseID LIKE ?");
            params.add("%" + courseID + "%");
        }

        // 根据课程名称模糊查询
        if (courseName != null && !courseName.isEmpty()) {
            sql.append(" AND courseName LIKE ?");
            params.add("%" + courseName + "%");
        }

        // 添加分页限制
        sql.append(" LIMIT ? OFFSET ?");
        params.add(size);
        params.add(offset);

        // 执行查询并返回结果
        return jdbcTemplate.query(sql.toString(), params.toArray(), (rs, rowNum) -> {
            Course course = new Course();
            course.setCourseID(rs.getString("courseID"));
            course.setClassID(rs.getString("classID"));
            course.setCourseName(rs.getString("courseName"));
            course.setYear(rs.getString("year"));
            course.setTerm(rs.getString("term"));
            course.setHour(rs.getInt("hour"));
            return course;
        });
    }
}
